package com.aidex.biz.service.impl;

import com.aidex.biz.domain.Order;
import com.aidex.biz.service.JdKuaiyunService;
import com.aidex.common.core.domain.entity.Company;
import com.aidex.system.service.CompanyService;
import com.lop.open.api.sdk.DefaultDomainApiClient;
import com.lop.open.api.sdk.LopException;
import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.*;
import com.lop.open.api.sdk.domain.ECAP.CommonModifyCancelOrderApi.commonCancelOrderV1.CommonOrderCancelRequest;
import com.lop.open.api.sdk.plugin.LopPlugin;
import com.lop.open.api.sdk.plugin.factory.OAuth2PluginFactory;
import com.lop.open.api.sdk.request.ECAP.EcapV1OrdersCancelLopRequest;
import com.lop.open.api.sdk.request.ECAP.EcapV1OrdersCreateLopRequest;
import com.lop.open.api.sdk.response.ECAP.EcapV1OrdersCancelLopResponse;
import com.lop.open.api.sdk.response.ECAP.EcapV1OrdersCreateLopResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 京东快运服务
 */
@Service
public class JdKuaiyunServiceImpl implements JdKuaiyunService {


    /**
     * 固定的收件人信息
     */
    public static final String receiverName = "优洁客武汉工厂";
    public static final String receiverPhone = "15369521144";
    public static final String receiverAddress = "湖北省武汉市新洲区双柳街道坤腾洗涤中心优洁客洗衣工厂2号厂房";

    public static final String jdAppKey = "5f0732f6342f4bac9b3c6e42cbb0ee0a";
    public static final String jdAppSecret = "6165ad47f7db4de09129807c6e702675";

    public static final String jdToken = "3d06830db77a4b73b84c76f48bfed671";
            //test "e70bc3c469b0491eb895814efd0d4de2";
            // "3d06830db77a4b73b84c76f48bfed671";
    // 月结号
    public static final String jdYueNo = "027K2888902";


    public static final String jdApiUrl_Test = "https://uat-api.jdl.com";

    public static final String jdApiUrl = "https://api.jdl.com";

    @Autowired
    private CompanyService companyService;

    /**
     * 洗护订单给京东发送快运信息
     *
     * @param order
     * @return
     */
    @Override
    public EcapV1OrdersCreateLopResponse sendJdKuaiYuanOrder(Order order) throws LopException, ParseException {

        DefaultDomainApiClient client = new DefaultDomainApiClient(jdApiUrl, 500, 15000);
        //入参对象（请记得更换为自己要使用的接口入参对象）
        EcapV1OrdersCreateLopRequest request = new EcapV1OrdersCreateLopRequest();
        CommonCreateOrderRequest commonCreateOrderRequest = new CommonCreateOrderRequest();

        // 订单号
        commonCreateOrderRequest.setOrderId(order.getId());
        // 寄件人信息
        Contact senderContact = new Contact();
        senderContact.setName(order.getLianxiName());
        senderContact.setPhone(order.getPhone());
        senderContact.setFullAddress(order.getRoomAddress());
        commonCreateOrderRequest.setSenderContact(senderContact);
        // 收件人信息
        Contact receiverContact = new Contact();
        receiverContact.setName(receiverName);
        receiverContact.setPhone(receiverPhone);
        receiverContact.setFullAddress(receiverAddress);
        commonCreateOrderRequest.setReceiverContact(receiverContact);
        // orderOrigin 下单来源 0-c2c 1-b2c 2-c2b 4-kyb2c 5- kyc2c
        commonCreateOrderRequest.setOrderOrigin(1);
        // orderOrigin=2时，settleType必须是5，并且c2bAddedSettleTypeInfo必填
        commonCreateOrderRequest.setSettleType(3);
        C2BAddedSettleTypeInfo c2BAddedSettleTypeInfo=new C2BAddedSettleTypeInfo();
        c2BAddedSettleTypeInfo.setBasicFreigthSettleType(3);
        c2BAddedSettleTypeInfo.setPackageServiceSettleType(3);
        c2BAddedSettleTypeInfo.setGuaranteeMoneyServiceSettleType(3);
        commonCreateOrderRequest.setC2bAddedSettleTypeInfo(c2BAddedSettleTypeInfo);
        //客户编码，orderOrigin为 1 或者 2 时必填
        commonCreateOrderRequest.setCustomerCode(jdYueNo);
        // 产品信息
        CommonProductInfo commonProductInfo = new CommonProductInfo();
        // https://cloud.jdl.com/#/open-business-document/access-guide/267/54153
        commonProductInfo.setProductCode("ed-m-0001");

        //增值服务产品信息 https://cloud.jdl.com/#/open-business-document/access-guide/267/54155
        //List<CommonProductInfo> commonProductInfoList=new ArrayList<>();
        //commonProductInfo=new CommonProductInfo();
        //commonProductInfo.setProductCode("ed-a-0002");
        //Map<String,String> baojia=new HashMap<>();
        //baojia.put("","");
        //commonProductInfo.setProductAttrs(baojia);
        //commonProductInfoList.add(commonProductInfo);
        //commonProductInfo.setAddedProducts(commonProductInfoList);
        commonCreateOrderRequest.setProductsReq(commonProductInfo);

        List<CommonCargoInfo> commonCargoInfoList = new ArrayList<>();
        CommonCargoInfo commonCargoInfo = new CommonCargoInfo();
        commonCargoInfo.setName("服装");
        //commonCargoInfo.setQuantity(1);
        //commonCargoInfo.setWeight(new BigDecimal(1));
        //commonCargoInfo.setVolume(new BigDecimal(2));
        commonCargoInfoList.add(commonCargoInfo);
        commonCreateOrderRequest.setCargoes(commonCargoInfoList);
        // 产品信息
        List<CommonGoodsInfo> commonGoodsInfoList=new ArrayList<>();
        CommonGoodsInfo commonGoodsInfo=new CommonGoodsInfo();
        commonGoodsInfo.setName("清洗商品");
        commonGoodsInfo.setCode("UR000001");
        commonGoodsInfo.setAmount(new BigDecimal("1"));
        commonGoodsInfo.setQuantity(3);
        commonGoodsInfoList.add(commonGoodsInfo);
        commonCreateOrderRequest.setGoods(commonGoodsInfoList);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        commonCreateOrderRequest.setPickupStartTime(sdf.parse(order.getVisitBeginTime()));
        commonCreateOrderRequest.setPickupEndTime(sdf.parse(order.getVisitEndTime()));
        // 取件单号
        // commonCreateOrderRequest.setWaybillCode(order.getDeliveryNo());
        request.setRequest(commonCreateOrderRequest);
        // 公共参数按顺序分别为AppKey、AppSecret、AccessToken
        LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdAppKey, jdAppSecret, jdToken);
        request.addLopPlugin(lopPlugin);
        return client.execute(request);
    }

    /**
     * 订单取消
     * @param order
     * @return
     * @throws LopException
     */
    @Override
    public EcapV1OrdersCancelLopResponse cancelJdKuaiYuanOrder(Order order) throws LopException {
        DefaultDomainApiClient client = new DefaultDomainApiClient(jdApiUrl, 500, 15000);
        //入参对象（请记得更换为自己要使用的接口入参对象）
        EcapV1OrdersCancelLopRequest request = new EcapV1OrdersCancelLopRequest();
        CommonOrderCancelRequest commonOrderCancelRequest = new CommonOrderCancelRequest();
        commonOrderCancelRequest.setWaybillCode(order.getDeliveryNo());
        commonOrderCancelRequest.setOrderOrigin(2);
        commonOrderCancelRequest.setCustomerCode(jdYueNo);
        commonOrderCancelRequest.setCancelReason("用户取消");
        commonOrderCancelRequest.setCancelReasonCode("1");
        request.setRequest(commonOrderCancelRequest);
        LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdAppKey, jdAppSecret, jdToken);
        request.addLopPlugin(lopPlugin);
        return client.execute(request);
    }

    @Override
    public EcapV1OrdersCreateLopResponse companySendJdKuaiYuan(Company company,String orderId) throws LopException,
            ParseException {

        DefaultDomainApiClient client = new DefaultDomainApiClient(jdApiUrl, 500, 15000);
        //入参对象（请记得更换为自己要使用的接口入参对象）
        EcapV1OrdersCreateLopRequest request = new EcapV1OrdersCreateLopRequest();
        CommonCreateOrderRequest commonCreateOrderRequest = new CommonCreateOrderRequest();
        // 订单号
        commonCreateOrderRequest.setOrderId(orderId);
        // 寄件人信息
        Contact senderContact = new Contact();
        senderContact.setName(company.getCompanyName());
        senderContact.setPhone(company.getLianxiPhone());
        senderContact.setFullAddress(company.getLianxiAddress());
        commonCreateOrderRequest.setSenderContact(senderContact);
        // 收件人信息
        Contact receiverContact = new Contact();
        receiverContact.setName(receiverName);
        receiverContact.setPhone(receiverPhone);
        receiverContact.setFullAddress(receiverAddress);
        commonCreateOrderRequest.setReceiverContact(receiverContact);
        commonCreateOrderRequest.setOrderOrigin(1);
        commonCreateOrderRequest.setSettleType(3);
        C2BAddedSettleTypeInfo c2BAddedSettleTypeInfo=new C2BAddedSettleTypeInfo();
        c2BAddedSettleTypeInfo.setBasicFreigthSettleType(3);
        c2BAddedSettleTypeInfo.setPackageServiceSettleType(3);
        c2BAddedSettleTypeInfo.setGuaranteeMoneyServiceSettleType(3);
        commonCreateOrderRequest.setC2bAddedSettleTypeInfo(c2BAddedSettleTypeInfo);
        commonCreateOrderRequest.setCustomerCode(jdYueNo);
        CommonProductInfo commonProductInfo = new CommonProductInfo();
        commonProductInfo.setProductCode("ed-m-0001");
        commonCreateOrderRequest.setProductsReq(commonProductInfo);
        List<CommonCargoInfo> commonCargoInfoList = new ArrayList<>();
        CommonCargoInfo commonCargoInfo = new CommonCargoInfo();
        commonCargoInfo.setName("服装");
        commonCargoInfoList.add(commonCargoInfo);
        commonCreateOrderRequest.setCargoes(commonCargoInfoList);
        // 产品信息
        List<CommonGoodsInfo> commonGoodsInfoList=new ArrayList<>();
        CommonGoodsInfo commonGoodsInfo=new CommonGoodsInfo();
        commonGoodsInfo.setName("清洗商品");
        commonGoodsInfo.setCode("UR000001");
        commonGoodsInfo.setAmount(new BigDecimal("1"));
        commonGoodsInfo.setQuantity(3);
        commonGoodsInfoList.add(commonGoodsInfo);
        commonCreateOrderRequest.setGoods(commonGoodsInfoList);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, 1);
        commonCreateOrderRequest.setPickupStartTime(calendar.getTime());
        calendar.add(Calendar.HOUR_OF_DAY, 1);
        commonCreateOrderRequest.setPickupEndTime(calendar.getTime());
        request.setRequest(commonCreateOrderRequest);
        // 公共参数按顺序分别为AppKey、AppSecret、AccessToken
        LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdAppKey, jdAppSecret, jdToken);
        request.addLopPlugin(lopPlugin);
        return client.execute(request);
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        System.out.println(sdf.parse("2023-10-17 13:00"));
    }
}
